/********************** (C) COPYRIGHT 2007-2010 RAISONANCE ********************
*
* File Name          :  Application.c
* Description        :  Circle_App CircleOS application template.
*
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "circle_api.h"
#include "ClockCounter.h"
#include "Sound.h"
#include "FFT.h"

/* Private defines -----------------------------------------------------------*/

// The following should be the minimal CircleOS version needed by your application
#define NEEDEDVERSION "V 4.0"

/* Private functions ---------------------------------------------------------*/
static enum MENU_code MsgVersion(void);

/* Public variables ----------------------------------------------------------*/
const char Application_Name[8+1] = {"PSound 1"};      // Max 8 characters

/*******************************************************************************
* Function Name  : Application_Ini
* Description    : Initialization function of Circle_App. This function will
*                  be called only once by CircleOS.
* Input          : None
* Return         : MENU_CONTINUE_COMMAND
*******************************************************************************/
enum MENU_code Application_Ini(void) {
    // Ensure that the current OS version is recent enough
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0) {
        return MsgVersion();
    }
    LCD_SetOffset(OFFSET_OFF);

#if 1
    DRAW_DisplayStringWithMode( 0, 200, Application_Name, ALL_SCREEN, NORMAL_TEXT, CENTER);
#endif
    
    // Your can set the frequency of the call by the system.
    UTIL_SetPll(SPEED_VERY_HIGH);
    MENU_SetAppliDivider(1000); // (100 = 33.33 ms handler frequency, 50=16.67ms)
    CycleCounter_Init();

    FFT_Init();
    
    AUDIO_SetMode(AUDIO_CIRCULAR_MODE, LG_16_BITS, FRQ_44KHZ, STEREO);
    Sound_FillBuffer();               
    AUDIO_Play(Sound_Get_AudioBuffer(), Sound_Get_AudioBuffer_Size() / 2);
    
    return MENU_CONTINUE_COMMAND;
 }

/*******************************************************************************
* Function Name  : Application_Handler
* Description    : Management of the Circle_App. This function will be called
*                  every millisecond by CircleOS while it returns MENU_CONTINUE.
* Input          : None
* Return         : MENU_CONTINUE
*******************************************************************************/
enum MENU_code Application_Handler(void) {
    static uint16_t counter = 0;
    //STOPWATCH_STOP
    //CycleCounter_Print(0, 90, 10);
    counter++;
    if(counter==10000)
        counter=0;
    
    // If the button is pressed, the application is exited
    if (BUTTON_GetState() == BUTTON_PUSHED) {
        BUTTON_WaitForRelease();
        AUDIO_Playback_Stop();
        return MENU_Quit();
    }

    FFT_Calc();
    //STOPWATCH_START
    return Sound_FillBuffer();   // Returning MENU_LEAVE will quit to CircleOS
    //return MENU_CONTINUE;
 }

/*******************************************************************************
* Function Name  : MsgVersion
* Description    : Displays the current CircleOS version and the version needed
*                  exit to main menu after 4 seconds
* Input          : None
* Return         : MENU_REFRESH
*******************************************************************************/
static enum MENU_code MsgVersion(void) {
    u8 hh, mm, ss, ss2;

    DRAW_DisplayString( 5,  60, "CircleOS", 17 );
    DRAW_DisplayString( 80, 60, UTIL_GetVersion(), 6 );
    DRAW_DisplayString( 5,  34, NEEDEDVERSION, 6 );
    DRAW_DisplayString( 50, 34, " required", 12 );

    RTC_GetTime( &hh, &mm, &ss );
    ss = ss + 4;
    ss = ss % 60;

    do {
        RTC_GetTime( &hh, &mm, &ss2 );
    } while ( ss2 != ss );           // do while < 4 seconds

    DRAW_Clear();
    return MENU_REFRESH;
}
